use crate::user::User;
use core::result::Result;
use mysql::prelude::*;
use mysql::*;

pub fn select_by_id(mut conn: PooledConn, id: i32) -> Result<Option<User>, Error> {
    conn.exec_first(
        "select * from user where id = :id",
        params! {
            "id" => id
        },
    )
    .map(|row| {
        row.map(|(id, name, age, email)| User {
            id,
            name,
            age,
            email,
        })
    })
}

pub fn select_all(mut conn: PooledConn) -> Vec<User> {
    conn.query_map("select * from user", |(id, name, age, email)| User {
        id,
        name,
        age,
        email,
    })
    .expect("Query failed.")
}
